package com.bestdata.forward.httptool.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bestdata.forward.api.LZInterfaceApi;
import com.bestdata.forward.cachecenter.CacheCenterFactory;
import com.bestdata.forward.httptool.HttpPostSendService;
import com.bestdata.forward.httptool.HttpRequestService;
import com.bestdata.forward.httptool.ReturnCodeCenter;
import com.bestdata.forward.model.BaseParamDTO;
import com.bestdata.forward.model.DeviceDTO;
import com.bestdata.forward.model.SensorConfigDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author chizy
 * @date 2021年1月18日18:17:28
 * @version 1.0.0
 * @description 裂缝数据发送实现类，使用value计算测值
 */
public class HttpPostSendCrackMeterServiceImpl implements HttpPostSendService {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    @Override
    public void httpSend(SensorConfigDTO sensorConfigDTO, DeviceDTO deviceDTO){

        /*List<DeviceSensorDTO> sensors = deviceDTO.getSensors();

        Map<String, String> map = new HashMap<>();
        sensors.stream().forEach(deviceSensorDTO -> {
            //将传感器测值封装
            map.put(deviceSensorDTO.getSenType(), deviceSensorDTO.getSv());
        });*/
        try {
            Map<String, Object> paramMap = new LinkedHashMap<>();

            paramMap.put("sensorCode", sensorConfigDTO.getSensorCode());
            Map<String, String> collectDataMap = new LinkedHashMap<>();
            collectDataMap.put("sensorId", sensorConfigDTO.getSensorId());
            collectDataMap.put("originalCrackDis", "");
            collectDataMap.put("crackDis", deviceDTO.getValue());
            collectDataMap.put("temp", "");
            collectDataMap.put("collectTime", sdf.format(Long.parseLong(deviceDTO.getTm())));

            Object[] x = new Object[]{collectDataMap};
            paramMap.put("info", x);
            Map<String, Object> requestMap = new LinkedHashMap<>();
            requestMap.put("data", JSON.toJSONString(paramMap));

            String res = HttpRequestService.doPost(LZInterfaceApi.uploadDataUrl, requestMap,
                    CacheCenterFactory.globalTokenMap.get(deviceDTO.getBridgeCodeName()));

            //返回参数反序列化
            BaseParamDTO baseParamDTO = JSONObject.parseObject(res, BaseParamDTO.class);
            //判断返回码状态
            boolean status = ReturnCodeCenter.getReturnCodeStatus(baseParamDTO.getCode(), requestMap, deviceDTO.getBridgeCodeName());

            if (status) {
                logger.error("裂缝计数据发送:" + "发送数据:" + JSON.toJSON(paramMap) + "返回值:" + res);
            }
        } catch (Exception e){
            e.printStackTrace();
            return;
        }
    }
}
